package top.infopub.mgr.cap.service.impl;

import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import top.infopub.generic.GenericDao;
import top.infopub.generic.GenericServiceImpl;
import top.infopub.mgr.cap.dao.PassFinAttDao;
import top.infopub.mgr.cap.dao.PassFinDao;
import top.infopub.mgr.cap.domain.PassFinDomain;
import top.infopub.model.cap.PassFin;
import top.infopub.model.cap.PassFinAtt;
import top.infopub.mgr.cap.service.PassFinService;
import top.infopub.mgr.cap.vo.PassFinVo;
import top.infopub.mgr.flow.cnst.FlowTemplateEnum;
import top.infopub.mgr.flow.service.WsFlowProcessService;

@Service
public class PassFinServiceImpl extends GenericServiceImpl<PassFin, String> implements
		PassFinService {

	@Autowired
	private PassFinDao passFinDao;
	
	@Autowired
	private PassFinAttDao passFinAttDao;
	
	@Autowired
    private WsFlowProcessService wsFlowProcess;
	
	@Override
	public GenericDao<PassFin, String> getDao() {
		return passFinDao;
	}

    @Override
    public Page<PassFinDomain> selectByDomainWhere(Page<PassFinDomain> page, PassFinVo passFinVo) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        return (Page<PassFinDomain>)passFinDao.selectByDomainWhere(passFinVo);
    }

    @Override
    public Page<PassFinDomain> selectMyCheckByDomainWhere(Page<PassFinDomain> page,
                                                          PassFinVo passFinVo) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        return (Page<PassFinDomain>)passFinDao.selectMyCheckByDomainWhere(passFinVo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void savePassFin(PassFin passFin, List<PassFinAtt> passFinAtts) {
        
        //执行新增
        //添加审批通过信息
        passFinDao.insertById(passFin);
        
        //批量添加审批通过附件
        if(CollectionUtils.isNotEmpty(passFinAtts)){
            passFinAttDao.insertByBatch(passFinAtts);
        }
        
        // 注意 这里根据不同的业务分支判断条件 获得不同enum
        FlowTemplateEnum templateEnum = FlowTemplateEnum.CAP_PASS_FIN;
        // 处理工作流数据
        wsFlowProcess.launch(templateEnum, passFin.getId(), passFin.getProcessId(), null);
    }
    
    @Override
    public PassFinVo selectById(String id){
        return passFinDao.selectById(id);
    }

    @Override
    public List<PassFinDomain> selectAllByCodes(String codes) {
        return passFinDao.selectAllByCodes(codes);
    }

    @Override
    public List<PassFin> queryByProCode(String proCode) {
        return passFinDao.queryByProCode(proCode);
    }

    @Override
    public List<PassFinDomain> getPassFinAll(String proCode) {
        return passFinDao.getPassFinAll(proCode);
    }

    @Override
    public List<PassFinDomain> getPassFin() {
        return passFinDao.getPassFin();
    }

}